package com.zzq.my.jetpacks.jectpackapp.ui.act

import android.text.TextUtils
import android.view.View
import by.kirich1409.viewbindingdelegate.viewBinding
import com.aisier.architecture.base.BaseActivity
import com.google.gson.Gson
import com.zzq.my.jetpacks.jectpackapp.R
import com.zzq.my.jetpacks.jectpackapp.bean.MapTest
import com.zzq.my.jetpacks.jectpackapp.databinding.Activity7Binding
import com.zzq.my.jetpacks.util.JsonTools


/**
 * 描述： json 数组 (JSONArray) 根据某个共同字段进行分组
 * 作者：zhuangzeqin
 * 时间: 2022/1/18-17:04
 * 邮箱：zzq@eeepay.cn
 * 备注:
 * [
{"id":"1001","name":"值1","value":"111"},
{"id":"1001","name":"值1","value":"11111"},
{"id":"1002","name":"值2","value":"25462"},
{"id":"1002","name":"值2","value":"23131"},
{"id":"1002","name":"值2","value":"2315432"},
{"id":"1003","name":"值3","value":"333333"}
]
装换为
根据相同 id 字段分组，转换成下面这种形式：


[
{
"id": "1001",
"data": [
{"id": "1001", "name": "值1", "value": "111"},
{ "id": "1001", "name": "值1", "value": "11111"}
]
},
{
"id": "1002",
"data": [
{ "id": "1002",  "name": "值2", "value": "25462" },
{ "id": "1002", "name": "值2", "value": "23131"},
{"id": "1002", "name": "值2","value": "2315432" }
]
},
{
"id": "1003",
"data": [
{"id": "1003", "name": "值3", "value": "333333" }
]
}
]
 */
class ActivityGroupingBy : BaseActivity(R.layout.activity_7), View.OnClickListener {
    private val binding by viewBinding(Activity7Binding::bind)

//    val collect: Map<String, List<MapTest>> =
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//            list.stream().collect(Collectors.groupingBy(MapTest::id))
//        } else {
//            //分组。即根据条件把集合拆分为为一个Map<K,List>类型的集合
//            list.groupBy {
//                it.id.toString()
//            }
//        }

    /**
     * 初始化数据
     */
    override fun initData() {
        val json = """[
    {
        "teamId":"310030",
        "id":"1001",
        "name":"值1",
        "value":"111"
    },
    {
        "teamId":"310030",
        "id":"1001",
        "name":"值1",
        "value":"11111"
    },
    {
        "teamId":"310030",
        "id":"1002",
        "name":"值2",
        "value":"25462"
    },
    {
        "teamId":"310030",
        "id":"1002",
        "name":"值2",
        "value":"23131"
    },
    {
        "teamId":"310030",
        "id":"1002",
        "name":"值2",
        "value":"2315432"
    },
    {
        "teamId":"310030",
        "id":"",
        "name":"值3",
        "value":"333333"
    },
    {
        "teamId":"310031",
        "id":"1004",
        "name":"值x1",
        "value":"23131"
    },
    {
        "teamId":"310031",
        "id":"1004",
        "name":"值x2",
        "value":"2315432"
    }
]
"""

//        val list: List<MapTest> = Gson().fromJson(json, object : TypeToken<List<MapTest>>() {}.type)
        val list: List<MapTest> = JsonTools.parserJsonToArrayBeans(json, MapTest::class.java)
        val str1 = list.joinToString(separator = ",") {
            it.value.toString()
        }
        val find = list.find {
            it.id.toString() == "aaaa"
        }
        list.any { it.id.toString() == "aaaa" }
        println("aa = $str1")
        val str2 = TextUtils.join(",", list.map { it.value })
        val str3 = TextUtils.join(",", list.toTypedArray())
        println("aa = $str2")
        println("aa = $str3")
        //teamId 为key  MapTest 为value
        val collect = list.groupBy { it.teamId.toString() }
//        val map= list.groupBy { it.teamId.toString() }.mapValues { (_,values)-> values.groupBy { it.id.toString() }}
        val map = list.groupBy { it.teamId.toString() }.mapValues { (_, values) ->
            values.associateBy {
                //如果id 为空 则取value 作为id 静心分组
                if (TextUtils.isEmpty(it.id.toString())) it.value.toString() else it.id.toString()
            }
        }
//        val mapValues = collect.mapValues {
//            it.value.groupBy { m ->
//                m.id.toString()
//            }
//        }
        val collect2 = mutableMapOf<String, Map<String, List<MapTest>>>();
        for ((key, value) in collect) {
            collect2[key] = value.groupBy { it.id.toString() }
        }
        val s = Gson().toJson(map)
        println("s = $s")
        val str = JsonTools.parseJsonToMaps<MapTest>(s)
        println("str1 = ${str}")
        println("str2 = ${JsonTools.parseMapToJson(str)}")

    }

    /**
     * 初始化操作
     */
    override fun initView() {

    }

    /**
     * 初始化点击事件操作
     */
    override fun eventOnClick() {

    }

    override fun onClick(v: View) {

    }

}